﻿using System.Numerics;
using Microsoft.EntityFrameworkCore;

namespace _19EF悲观并发控制;

class Program
{
    static async Task  Main(string[] args)
    {
        
        using MyDbContext ctx = new MyDbContext();
        
        using (var tx = await ctx.Database.BeginTransactionAsync())
        {
            var house = ctx.Houses.FromSqlInterpolated($"select * from t_houses where id=1 for update").First();
            Console.WriteLine("输入住户名称：");
            string name = Console.ReadLine();
            if (string.IsNullOrEmpty(house.Owner))
            {
                house.Owner = name;
                Console.WriteLine($"抢到了！当前户主是【{name}】");
            }
            else
            {
                if (house.Owner!=name)
                {
                    Console.WriteLine($"已经有人了：{house.Owner}");
                }
            }
            Thread.Sleep(5000);
            ctx.SaveChanges();
            tx.Commit();
            Console.ReadLine();
        }
        
    }
}